package com.bluefish.bltuan.action.user;

import java.text.MessageFormat;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.OpenAction;
import com.bluefish.bltuan.cache.ApplicationConstants;
import com.bluefish.bltuan.cache.ApplicationUtils;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.mail.EmailTemplate;
import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.EmailService;
import com.bluefish.bltuan.service.SubscriberService;
import com.bluefish.bltuan.tools.ActionUtil;
import com.bluefish.bltuan.util.ActiveUtils;
import com.bluefish.bltuan.util.StringUtils;

public class RegisterAction extends OpenAction {
    /**
     * 
     */
    private static final long serialVersionUID = 2267356603233513670L;

    //private UserService userService;

    private CategoryService   categoryService;

    private EmailService      emailService;

    private SubscriberService subscriberService;

    private Boolean           sub;

    private String            username;

    private String            email;

    private String            password;

    private String            cityId;

    public void setCityId(String cityId) {
        this.cityId = cityId;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Override
    public HttpHeaders index() {
        super.index();
        return new DefaultHttpHeaders(INDEX).disableCaching();
    }

    @Override
    public HttpHeaders create() {
        User user = new User();
        user.setEmail(this.email);
        user.setUsername(this.username);
        user.setPassword(this.password);
        Category category = this.categoryService.findById(Integer.valueOf(cityId));
        String activeCode = ActiveUtils.getActiveCode();
        user.setCity(category);
        user.setRight();
        user.setActiveCode(activeCode);
        userService.save(user);
        String activeLink = getActiveLink(activeCode, user.getEmail());
        EmailTemplate template = ApplicationUtils.get(ApplicationConstants.REGISTER_TEMPLATE);
        sendRegisterEmail(user, activeLink, template);
        if (this.sub != null && this.sub.booleanValue()) {
            this.subscriberService.subscribe(user.getEmail(), category);
        }
        this.setMessage(StringUtils.REGISTER_SUCCESS);
        return new DefaultHttpHeaders(SUCCESS).disableCaching();
    }

    private String getActiveLink(String activeCode, String username) {
        String base = "http://localhost/bltuan";
        String activeLink = base + "/user/active?email={0}&code={1}";
        return MessageFormat.format(activeLink, username, activeCode);
    }

    /**
     * 异步发送注册激活邮件
     * @param template 
     */
    private void sendRegisterEmail(final User user, final String activeLink,
                                   final EmailTemplate template) {
        final EmailService emailService = this.emailService;
        Thread t = new Thread() {
            @Override
            public void run() {
                emailService.sendRegisterEmail(user, activeLink, template);
            }
        };
        t.start();
    }

    /**
     * 检测email和用户名
     */
    @Override
    public HttpHeaders show() {
        if ("email-check".equals(getId())) {
            User user = this.userService.getByEmail(this.email);
            String result = (user == null) ? "Y" : "N";
            ActionUtil.writeResponse(result);
        } else if ("name-check".equals(getId())) {
            User user = this.userService.getByName(this.username);
            String result = (user == null) ? "Y" : "N";
            ActionUtil.writeResponse(result);
        }
        return null;
    }

    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void setSub(Boolean sub) {
        this.sub = sub;
    }

    public void setSubscriberService(SubscriberService subscriberService) {
        this.subscriberService = subscriberService;
    }
}
